package com.ruoyi.tester.executor;

import com.mashape.unirest.http.exceptions.UnirestException;
import com.ruoyi.tester.domain.TaskInterfaceEffectiveInfo;
import com.ruoyi.tester.executor.http.HttpUtils;
import com.ruoyi.tester.executor.http.HttpUtilsFactory;
import com.ruoyi.tester.executor.http.XzfHttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author SongJun
 * @description TODO:
 * @date 2021/4/12 15:12
 */
public class InterfaceCallUtils {
    private static Logger logger = LoggerFactory.getLogger(InterfaceCallUtils.class);

    public static XzfHttpResult execute(TaskInterfaceEffectiveInfo interfaceInfo, Map<String, String> headers) throws UnirestException {
        String finalUrl = composeUrl(interfaceInfo.getHost(), interfaceInfo.getPort(), interfaceInfo.getMapping());
        interfaceInfo.setFinalUrl(finalUrl);
        HttpUtils httpUtils = HttpUtilsFactory.getHttpUtils();
        String contentType = StringUtils.hasLength(interfaceInfo.getContentType()) ? interfaceInfo.getContentType() : "application/x-www-form-urlencoded";
        if("GET".equalsIgnoreCase(interfaceInfo.getHttpMethod())){
            XzfHttpResult result = httpUtils.get(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getPathParam());
            return result;
        } else if("HEAD".equalsIgnoreCase(interfaceInfo.getHttpMethod())){
            XzfHttpResult result = httpUtils.head(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getPathParam());
            return result;
        } else if ("POST".equalsIgnoreCase(interfaceInfo.getHttpMethod())) {
            XzfHttpResult result = httpUtils.post(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getBodyParam(), interfaceInfo.getPathParam());
            return result;
        } else if ("PUT".equalsIgnoreCase(interfaceInfo.getHttpMethod())) {
            XzfHttpResult result = httpUtils.put(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getBodyParam(), interfaceInfo.getPathParam());
            return result;
        } else if ("DELETE".equalsIgnoreCase(interfaceInfo.getHttpMethod())) {
            XzfHttpResult result = httpUtils.delete(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getBodyParam(), interfaceInfo.getPathParam());
            return result;
        } else if ("PATCH".equalsIgnoreCase(interfaceInfo.getHttpMethod())) {
            XzfHttpResult result = httpUtils.patch(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getBodyParam(), interfaceInfo.getPathParam());
            return result;
        } else if ("TRACE".equalsIgnoreCase(interfaceInfo.getHttpMethod())) {
            XzfHttpResult result = httpUtils.trace(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getBodyParam(), interfaceInfo.getPathParam());
            return result;
        } else if ("OPTIONS".equalsIgnoreCase(interfaceInfo.getHttpMethod())) {
            XzfHttpResult result = httpUtils.options(finalUrl, contentType, headers, interfaceInfo.getQueryParam(), interfaceInfo.getBodyParam(), interfaceInfo.getPathParam());
            return result;
        } else {
            return null;
        }
    }

    public static String composeUrl(String host, Integer port, String mapping){
        StringBuilder sb = new StringBuilder();
        if(!host.startsWith("http")){
            sb.append("http://");
        }
        sb.append(host);
        if(sb.charAt(sb.length()-1) == '/'){
            sb.deleteCharAt(sb.length()-1);
        }
        if(port != null){
            sb.append(":").append(port);
        }
        if(mapping.charAt(0) != '/'){
            sb.append('/');
        }
        sb.append(mapping);
        if(sb.charAt(sb.length()-1) == '/'){
            sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }

}
